﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization.Json;

namespace Serene.Storage
{
    public class SereneStorage : IDisposable
    {
        private const string StatusFile = "SereneStatus.json";
        private const string CriteriaFile = "SereneCriteria.json";
        private readonly IsolatedStorageFile _storage = IsolatedStorageFile.GetUserStoreForApplication();
        private Status _status;
        readonly DataContractJsonSerializer _statusSerializer = new DataContractJsonSerializer(typeof(Status));
        readonly DataContractJsonSerializer _criteriaSerializer = 
            new DataContractJsonSerializer(typeof(Dictionary<string, string>));

        public IDictionary<string, string> CriteriaStorage { get; private set; }

        public Status LoadStatus()
        {
            if(_storage.FileExists(StatusFile))
            {
                try
                {
                    using (var file = _storage.OpenFile(StatusFile, FileMode.Open))
                    {
                        _status = _statusSerializer.ReadObject(file) as Status;
                    }
                }
                catch(Exception ex)
                {
                    _storage.DeleteFile(StatusFile);
                    Debug.WriteLine(ex.Message);
                    if(Debugger.IsAttached)
                        Debugger.Break();
                }
            }
            else
            {
                _status = new Status {InstallationDate = DateTime.Now};
            }
            return _status;
        }

        internal void LoadCriteriaSettings()
        {
            if(_storage.FileExists(CriteriaFile))
            {
                try
                {
                    using(var file = _storage.OpenFile(CriteriaFile, FileMode.Open))
                    {
                        CriteriaStorage = _criteriaSerializer.ReadObject(file) as IDictionary<string,string>;
                    }
                }
                catch(Exception ex)
                {
                    _storage.DeleteFile(CriteriaFile);
                    Debug.WriteLine(ex.Message);
                    if(Debugger.IsAttached)
                        Debugger.Break();
                }
            }
            if(CriteriaStorage == null)
                CriteriaStorage = new Dictionary<string, string>();

        }

        internal void SaveStatus(Status status)
        {
            using(var file = _storage.OpenFile(StatusFile, FileMode.Create))
            {
                _statusSerializer.WriteObject(file, status);
            }
        }

        internal void SaveCriteria()
        {
            using(var file = _storage.OpenFile(CriteriaFile, FileMode.Create))
            {
                _criteriaSerializer.WriteObject(file, CriteriaStorage);
            }
        }

        public void Dispose()
        {
            _storage.Dispose();
        }
    }
}
